<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> 消防主机 </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'device:management:inFireHost:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.equipmentName }}
        </a>
      </template>
      <!-- 新增actionColumn插槽 -->
      <template #actionColumn="{ record }">
        <a-button @click="handleForm({ tid: record.tid })" v-auth="'device:management:inFireHost:edit'" type="text">
         <span style="font-size:14px; color: #52c41a;">编辑</span>
        </a-button>
        <Popconfirm
          title="是否确认删除该记录？"
          ok-text="确认"
          cancel-text="取消"
          @confirm="handleDelete(record)"
        >
          <a-button v-auth="'device:management:inFireHost:edit'" type="text">
            <span style="font-size:14px;color: red">删除</span>
          </a-button>
        </Popconfirm>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsDeviceManagementInFireHostList">
import { unref, ref } from 'vue';
import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage';
import { router } from '/@/router';
import { Icon } from '/@/components/Icon';
import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
import { inFireHostDelete, inFireHostListData } from '/@/api/device.management/inFireHost';
import { unitEnterpriseDominantListData } from '/@/api/unitmanage/unitEnterpriseDominant';
import { useDrawer } from '/@/components/Drawer';
import { FormProps } from '/@/components/Form';
import { officeTreeData } from '/@/api/sys/office';
import InputForm from './form.vue';
import { message,Popconfirm } from 'ant-design-vue';

const { t } = useI18n('device.management.inFireHost');
const { showMessage } = useMessage();
const { meta } = unref(router.currentRoute);

const getTitle = {
  icon: meta.icon || 'i-ant-design:book-outlined',
  value: meta.title || t('消防主机管理表管理'),
};
const EnterpriseParams = ref<Recordable>({
  name: ''
});

const EquipmentNameParams = ref({
  equipmentName: ''
})

const searchForm: FormProps = {
  baseColProps: { md: 8, lg: 6 },
  labelWidth: 90,
  schemas: [
    {
      label: t(''),
      field: 'enterpriseId',
      fieldLabel: 'enterpriseName',
      component: 'Select',
      componentProps: () => {
        return {
          placeholder: t('请输入设备所属主体名称'),
          api: unitEnterpriseDominantListData,
          params: EnterpriseParams.value,
          fieldNames: { label: 'name', value: 'unitCode' },
          resultField: 'list',
          notFoundContent: '未查询到记录',
          filterTreeNode: false,
          allowClear: true,
          filterOption: false,
          immediate: true,
          onSearch: (value: string) => {
            EnterpriseParams.value.name = value;
            console.log(EnterpriseParams.value);
          },
        };
      },
    },
    {
      label: t(''),
      field: 'equipmentName',
      fieldLabel: 'equipmentName',
      component: 'TreeSelect',
      componentProps: () => {
        return {
          placeholder: t('请输入消防主机名称'),
          api: inFireHostListData,
          params: EquipmentNameParams.value,
          fieldNames: { label: 'equipmentName', value: 'equipmentCode' },
          resultField: 'list',
          notFoundContent: '未查询到记录',
          filterTreeNode: false,
          allowClear: true,
          filterOption: false,
          immediate: true,
          onSearch: (value: string) => {
            EquipmentNameParams.value.equipmentName = value;
            console.log(EquipmentNameParams.value);
          },
        };
      },
    },
    {
      label: t(''),
      field: 'equipmentCode',
      component: 'Input',
      componentProps: {
        placeholder: t('请输入设备编号'),
        allowClear: true,
      },
    },
    {
      label: t(''),
      field: 'status',
      component: 'Select',
      componentProps: {
        placeholder: t('请输入设备状态'),
        options: [
          { label: t('正常'), value: '正常' },
          { label: t('异常'), value: '异常' },
          { label: t('24H无预警'), value: '24H无预警' }
        ],
        allowClear: true,
        onChange: handleSuccess,
      },
    },
  ],
};

const tableColumns: BasicColumn[] = [
  {
    title: t('消防主机'),
    dataIndex: 'equipmentName',
    key: 'a.equipment_name',
    sorter: true,
    width: 230,
    align: 'left',
    slot: 'firstColumn',
  },
  {
    title: t('设备编号'),
    dataIndex: 'equipmentCode',
    key: 'a.equipment_code',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('设备类型名称'),
    dataIndex: 'equipmentTypeName',
    key: 'a.equipment_type_name',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('卡号'),
    dataIndex: 'cardCode',
    key: 'a.card_code',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('状态'),
    dataIndex: 'status',
    key: 'a.status',
    sorter: true,
    width: 130,
    align: 'center',
    dictType: 'fire_host_type',

  },
  {
    title: t('最后一次采集时间'),
    dataIndex: 'testingLastTime',
    key: 'a.testing_last_time',
    sorter: true,
    width: 130,
    align: 'center',
  },
  {
    title: t('所属企业'),
    dataIndex: 'enterpriseName',
    key: 'a.enterprise_name',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('检测间隔'),
    dataIndex: 'testingInterval',
    key: 'a.testing_interval',
    sorter: true,
    width: 130,
    align: 'center',
  },
  {
    title: t('操作'),
    dataIndex: 'action',
    key: 'action',
    width: 160,
    fixed: 'right',
    slot: 'actionColumn' // 指定使用actionColumn插槽
  }
];

const [registerTable, { reload }] = useTable({
  api: inFireHostListData,
  beforeFetch: (params) => {
    return params;
  },
  columns: tableColumns,
  formConfig: searchForm,
  showTableSetting: true,
  useSearchForm: true,
  canResize: true,
});

const [registerDrawer, { openDrawer }] = useDrawer();

function handleForm(record: Recordable) {
  openDrawer(true, record);
}

async function handleDelete(record: Recordable) {
  const params = { tid: record.tid };
  const res = await inFireHostDelete(params);
  showMessage(res.message);
  handleSuccess(record);
}

function handleSuccess(record: Recordable) {
  reload({ record });
}


</script>